package stream;
import com.alibaba.fastjson.JSON;
import lambda.cart.CartService;
import lambda.cart.Sku;
import lambda.cart.SkuCategoryEnum;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * 对比原始集合操作与Stream集合操作
 * @author zhaowenxu
 * @date 2020/4/6 22:57
 */
public class StreamVs {

    List<Sku> skuList = CartService.getCartSkuList();

    /*
     * 需求：
     * 1. 想看看购物车中都有什么商品
     * 2. 图书类商品都给买
     * 3. 其余的商品中买两件最贵的
     * 4. 只需要两件商品的名称和总价
     */

    /**
     * 以原始集合操作实现需求
     */
    public void oldCartHandle() {
        // 1
        for (Sku sku : skuList) {
            System.out.println("--------------------1--------------------");
            System.out.println(JSON.toJSONString(sku, true));
        }
        // 2
        List<Sku> notBooksSkuList = new ArrayList<>();
        for (Sku sku : skuList) {
            if (!SkuCategoryEnum.BOOKS.equals(sku.getSkuCategory())) {
                notBooksSkuList.add(sku);
            }
        }
        // 3 排序
        System.out.println("--------------------2--------------------");
        System.out.println(JSON.toJSONString(notBooksSkuList, true));
        notBooksSkuList.sort((sku1, sku2) -> (int)(sku2.getSkuPrice() - sku1.getSkuPrice()));
        System.out.println("--------------------3--------------------");
        System.out.println(JSON.toJSONString(notBooksSkuList, true));
        // top2
        List<Sku> top2SkuList = new ArrayList<>();
        if (notBooksSkuList.size() > 1) {
            top2SkuList.add(notBooksSkuList.get(0));
            top2SkuList.add(notBooksSkuList.get(1));
        }
        // 4 商品名称和总价
        Double totalPrice = 0.00;
        List<String> skuNameList = new ArrayList<>();
        System.out.println("--------------------4--------------------");
        for (Sku sku : top2SkuList) {
            skuNameList.add(sku.getSkuName());
            totalPrice += sku.getSkuPrice();
        }
        System.out.println("总价:" + totalPrice);
        System.out.println(JSON.toJSONString(skuNameList, true));
    }

    /**
     * 以Stream流方式实现需求
     */
    public void newCartHandle() {
        AtomicReference<Double> totalPrice = new AtomicReference<>(0.00);
        List<String> resultSkuNameList;
        resultSkuNameList = skuList.stream()
                // 查看
                .peek(sku -> System.out.println(JSON.toJSONString(sku, true)))
                // 过滤
                .filter(sku -> !SkuCategoryEnum.BOOKS.equals(sku.getSkuCategory()))
                // 排序
                .sorted(Comparator.comparing(Sku::getSkuPrice).reversed())
                // top2
                .limit(2)
                // 累加商品价格
                .peek(sku -> totalPrice.set(totalPrice.get() + sku.getSkuPrice()))
                // 获取商品名称
                .map(Sku::getSkuName)
                // 收集结果
                .collect(Collectors.toList());
        // 打印结果
        System.out.println(JSON.toJSONString(resultSkuNameList, true));
        System.out.println("商品总价:" + totalPrice.get());

    }

    public static void main(String[] args) {
        //new StreamVs().oldCartHandle();
        new StreamVs().newCartHandle();
    }
}
